﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Next.CodeGen.Core.Generators
{
    public class Clean
    {
        public static void CleanAll()
        {
            Console.WriteLine("Cleaning all generated files...");
            CleanSchema();
            CleanForms(null);
            Console.WriteLine("All generated files cleaned successfully!");
        }
        public static void CleanSchema()
        {
            //INST : No DataBase on Dev Environment, avoid clean Schema
            Console.WriteLine("Error : Schema could not be cleaned!");
            //try
            //{
            //    Console.WriteLine("Cleaning schema...");
            //    System.IO.DirectoryInfo SchemaFolder = new System.IO.DirectoryInfo(@Properties.Settings.Default.SchemaPath);
            //    foreach (System.IO.FileInfo file in SchemaFolder.GetFiles())
            //    {
            //        file.Delete();
            //    }
            //    foreach (System.IO.DirectoryInfo subDirectory in SchemaFolder.GetDirectories())
            //    {
            //        subDirectory.Delete(true);
            //    }
            //    Console.WriteLine("Schema cleaned successfully!");
            //}
            //catch (Exception)
            //{
            //    Console.WriteLine("Error : Schema could not be cleaned!");
            //}
        }

        public static void CleanForms(List<string> Tables)
        {
            if (Tables != null && Tables.Count == 0)
            {
                Tables = null;
            }
            int CountDirectoriesDeleted = 0;
            try
            {
                Console.WriteLine("Cleaning forms...");
                System.IO.DirectoryInfo FormsFolder = new System.IO.DirectoryInfo(@Properties.Settings.Default.FormsPath);
                foreach (System.IO.FileInfo file in FormsFolder.GetFiles())
                {
                    file.Delete();
                }
                foreach (System.IO.DirectoryInfo subDirectory in FormsFolder.GetDirectories())
                {
                    if (Tables == null || Tables.Contains(subDirectory.Name))
                    {
                        CountDirectoriesDeleted++;
                        subDirectory.Delete(true);
                    }
                }
                Console.WriteLine(CountDirectoriesDeleted + " Forms cleaned successfully!");
            }
            catch
            {
                Console.WriteLine("Error : Schema could not be cleaned!");
            }
        }
    }
}
